import axios from "axios"

// 配置网络请求

const env = import.meta.env
console.log("env: ", env)
const instance = axios.create({
  baseURL: "https://calcapi.cretechsh.cn", //env.MODE === 'development' ? '/api' : env.VITE_BASE_URL,
  timeout: 1e4,
  withCredentials: true,  // 是否允许跨域
})

// 请求拦截
instance.interceptors.request.use((config) => {
  console.log("config: ", config)

  // TODO: 可以添加token等前置数据
  return config
}, (error) => {
  Promise.reject(error) // 请求错误
})


// 响应拦截
instance.interceptors.response.use((response) => {
  return response?.data
}, (error) => {
  return Promise.reject(error)
})


/**
 * 接口请求
 * @param {Object} param0 {请求方式，请求地址，数据，请求配置}
 * @returns 返回的数据Promise
 */
function request ({ method, url, data, config }) {
  method = method && method.toLowerCase()
  if (method == 'post') {
    return instance.post(url, data, { ...config })
  } else if (method == 'get') {
    return instance.get(url, data, { ...config })
  } else if (method == 'put') {
    return instance.get(url, data, { ...config })
  } else if (method == 'delete') {
    return instance.get(url, data, { ...config })
  } else {
    console.log('位置的请求')
    return false
  }
}

export default request


